package com.lanhai.utils;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Optional;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * 对话框工具
 *
 * @author 机器人
 *
 */
public class DialogUtils {
	/**
	 * 信息提示
	 *
	 * @param message
	 */
	
	//public static Stage modal_dialog;
	
	public static Alert show(String message) {
		Alert alert = new Alert(AlertType.WARNING);
		alert.setTitle("提示");
		alert.setHeaderText(message);
		// alert.setContentText("Careful with the next step!");
		// alert.show();

		alert.showAndWait();
		return alert;
	}
	public static Alert show(String message, String title, String logoUrl) {
		Alert alert = new Alert(AlertType.WARNING);
		alert.setTitle(title);
		alert.setHeaderText(message);
		Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
		stage.getIcons().add(new Image(logoUrl));
		if(title.equals("关于系统")){
			alert.setGraphic(new ImageView(logoUrl));
		}
		alert.showAndWait();
		return alert;
	}
	/**
	 * 确认对话框
	 *
	 * @param response
	 */
	public static Alert sure(String message, DialogResponse response, String title, String logoUrl) {

		Alert alert = new Alert(AlertType.CONFIRMATION);
		alert.setTitle(title);
		alert.setHeaderText(message);
		Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
		stage.getIcons().add(new Image(logoUrl));
		// alert.setContentText("Are you ok with this?");
		Optional<ButtonType> result = alert.showAndWait();

		// System.out.println("对话框类型:" + result.get());
		if (result.get() == ButtonType.OK) {
			// ... user chose OK
			response.onOK();
		} else {
			// 关闭对话框
			alert.close();
			// ... user chose CANCEL or closed the dialog
			response.onCancel();
		}
		return alert;
	}

	/**
	 * 显示自定义对话框
	 */
	public static FXMLLoader showMyDialog(Stage stage, URL url) {
		// Dialog.fxml为对话框的fxml文件
		// URL url = getClass().getResource("Dialog.fxml");
		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.setLocation(url);
		try {
			fxmlLoader.load();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		Parent root = fxmlLoader.getRoot();

		Stage modal_dialog = new Stage(StageStyle.DECORATED);
		modal_dialog.setResizable(false);// 设置不可伸缩
		// modal_dialog.setTitle("新增分站ip");
		modal_dialog.initModality(Modality.WINDOW_MODAL);
		modal_dialog.initOwner(stage);
		Scene scene = new Scene(root);
		modal_dialog.setScene(scene);
		modal_dialog.show();
		// 对话框处理
		return fxmlLoader;

	}
	public static FXMLLoader showMyDialog(Stage stage, URL url, String logoUrl, String title
			, double width, double height) {
		FXMLLoader fxmlLoader = new FXMLLoader();
		//System.out.println(url);
		fxmlLoader.setLocation(url);
		try {
			fxmlLoader.load();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		Parent root = fxmlLoader.getRoot();

		Stage modal_dialog = new Stage(StageStyle.DECORATED);
		modal_dialog.getIcons().add(new Image(logoUrl));
		modal_dialog.setResizable(false);
		modal_dialog.setTitle(title);
		modal_dialog.initModality(Modality.WINDOW_MODAL);
		modal_dialog.initOwner(stage);
		Scene scene = new Scene(root);
		modal_dialog.setScene(scene);
		modal_dialog.setWidth(width);
		modal_dialog.setHeight(height);
		modal_dialog.show();
		return fxmlLoader;

	}
	
	/**
	 * 显示自定义对话框
	 */
	public static FXMLLoader showMyDialog(Stage modal_dialog, Stage parentStage, URL url) {
		// Dialog.fxml为对话框的fxml文件
		// URL url = getClass().getResource("Dialog.fxml");
		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.setLocation(url);
		try {
			fxmlLoader.load();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		Parent root = fxmlLoader.getRoot();
		
		modal_dialog.setResizable(false);// 设置不可伸缩
		// modal_dialog.setTitle("新增分站ip");
		modal_dialog.initModality(Modality.WINDOW_MODAL);
		modal_dialog.initOwner(parentStage);
		Scene scene = new Scene(root);
		modal_dialog.setScene(scene);
		modal_dialog.show();
		// 对话框处理
		return fxmlLoader;

	}
	
	/**
	 * 可选择对话框
	 */
	public static void choice(String message, List<Integer> choices, DialogChoiceResponse response) {
		if (choices.size() > 0) {

			ChoiceDialog<Integer> dialog = new ChoiceDialog<Integer>(choices.get(0), choices);
			dialog.setTitle("提示");

			dialog.setHeaderText(message);

			// Traditional way to get the response value.
			Optional<Integer> result = dialog.showAndWait();
			if (result.isPresent()) {
				System.out.println("Your choice: " + result.get());
				response.onChoice(result.get());
			}
		}
	}

	/**
	 * 对话框接口
	 *
	 * @author 机器人
	 *
	 */
	public interface DialogResponse {
		/**
		 * 确认
		 */
		public void onOK();

		/**
		 * 取消
		 */
		public void onCancel();
	}

	/**
	 * 对话框接口
	 *
	 * @author 机器人
	 *
	 */
	public interface DialogChoiceResponse {
		/**
		 * 下标
		 */
		public void onChoice(int index);

	}
}
